using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {
    internal class GenericMenuButton : Button {
        private GenericMenu menu;
        public GenericMenuButton(string text) {
            this.text = text;
            menu = new GenericMenu();
            // TestMenu();
            clicked += OnClick;
        }
        private void OnClick() {
            // var menuPos = clickable.lastMousePosition;
            // menuPos = this.LocalToWorld(menuPos);
            // menu.DropDown(new Rect(menuPos, Vector2.zero));

            var menuPos = this.LocalToWorld(new Vector2(0, worldBound.height + 5));
            menu.DropDown(new Rect(menuPos, Vector2.zero));
        }
        public void TestMenu() {
            // 添加菜单项
            menu.AddItem(new GUIContent("Say Hello"), false, () => Debug.Log("Hello!"));
            menu.AddItem(new GUIContent("Change Color"), false, () => style.color = Color.red);
            menu.AddSeparator(null);
            menu.AddItem(new GUIContent("Reset"), false, () => style.color = Color.black);
            menu.AddItem(new GUIContent("Parent/SubMenu/Item 1"), false, () => Debug.Log("Clicked Item 1"));
            menu.AddItem(new GUIContent("Parent/SubMenu/Item 2"), false, () => Debug.Log("Clicked Item 2"));
            menu.AddSeparator(null);
        }
        public void AddMenuItem(string path, GenericMenu.MenuFunction action) {
            menu.AddItem(new GUIContent(path), false, action);
        }
        public void AddMenuSeparator() {
            menu.AddSeparator(null);
        }
    }
}
